package hackathon.light.up;

import java.util.ArrayList;

import android.util.Log;
import android.view.MotionEvent;

import com.e3roid.E3Scene;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.event.ShapeEventListener;

public abstract class Element {

		public ArrayList<LightSource> lightSources;
		public boolean flagOnScene;
		public ArrayList<LightDirection> allowedDirections;
		public int row, col;
		public LightDirection orientation;

		public Sprite  robot;
		public static Texture robotTexture;
		
		public Element(){
			flagOnScene = false;
			lightSources = new ArrayList<LightSource>();
			allowedDirections = new ArrayList<LightDirection>();
			robot = new Sprite(robotTexture,0,0);
			float w = robot.getWidth();
			float h = robot.getHeight();
			robot.scale((float)Grid.CELL_SIZE/w,(float)Grid.CELL_SIZE/h);
			w = Grid.CELL_SIZE/(2*w);
			h = Grid.CELL_SIZE/(2*h);
			robot.move((int)-w, (int)-h);
			robot.addListener(new ShapeEventListener() {
				@Override
				public boolean onTouchEvent(E3Scene arg0, Shape arg1, MotionEvent arg2,
						int arg3, int arg4) {
					// TODO Auto-generated method stub
					Log.d("Sprite",arg2.getAction() + " "+ arg3+" "+arg4);
					if(arg2.getAction() == 1){
						float angle = robot.getAngle();
						robot.rotate(angle+45);
						orientation = LightDirection.values()[(orientation.ordinal() +7)%8];
						
						/*
						for(int i =1;i<Grid.elementMatrix.length;i++) {
							for(int j =1;j<Grid.elementMatrix[i].length;j++) {
								//for(int k =0;k<Grid.elementMatrix[i][j].lightSources.size();k++)
								if(Grid.elementMatrix[i][j] != null) {
									Grid.elementMatrix[i][j].removeLight(arg0);
									Grid.elementMatrix[i][j].reset();
								}
							}
						}		
						*/	
					}
					return true;
				}
			});
		}
		public Element(int x, int y){
			this();
			row = x;
			col = y;
			int w = robot.getWidth()/2;
			int h = robot.getHeight()/2;
			robot.getScaleX();
			robot.getScaleY();
			Log.d("patrat",w+ " " + h);
			robot.move(Grid.POSX  + (int)(col)*Grid.CELL_SIZE,Grid.POSY + (int)(row)*Grid.CELL_SIZE);
		}
		public Element(int x, int y,LightDirection dir){
			this();
			row = x;
			col = y;
			orientation = dir;		
			int w = robot.getWidth()/2;
			int h = robot.getHeight()/2;
			Log.d("patrat",w+ " " + h);
			robot.move(Grid.POSX  + (int)(col)*Grid.CELL_SIZE,Grid.POSY + (int)(row)*Grid.CELL_SIZE);
				
		}
		
		public void reset(){
		}
		
		public void addToScene(int x, int y){
			row = x;
			col = y;
			flagOnScene = true;
		}
		
		public void removeFromScene(){
			row = 0;
			col = 0;
			flagOnScene = false;
		}
		
		public void removeLight(E3Scene scene) {
			for( int i = 0; i<lightSources.size(); i++)  {
				if(lightSources.get(i).getLight() != null) {
				lightSources.get(i).getLight().scale(0, 0);
				}
			}
		}
		
		public abstract void update(LightSource lightSource);
		
		public void draw(E3Scene scene) {
			
			scene.addEventListener(robot);
			
			scene.getTopLayer().add(robot);
			for(int i =0; i <lightSources.size(); i++) {
				if(lightSources.get(i).flagOn)
					lightSources.get(i).draw(scene);
			}
		}
		
}
